﻿using System.Linq;
using UnityEngine;

namespace JH
{
    /// <summary>
    ///  图表中任意继承自Mono的组件的基类
    /// </summary>
    public abstract class BasicMono<TPoint> :
#if ODIN_INSPECTOR
        Sirenix.OdinInspector.SerializedMonoBehaviour,
#else
        UnityEngine.MonoBehaviour,
#endif
        IChartBase
        where TPoint : struct
    {
        protected virtual void OnValidate()
        {
            // 如果不是图表本身，则设置RectTransform
            if (!GetComponents<IChart<TPoint>>().Any()) this.SetRectTransform();
            
            // 刷新图表
            RefreshChart();
        }

        /// <summary>
        ///  刷新图表
        /// </summary>
        protected void RefreshChart()
        {
            var chart = this.GetChart<TPoint>();
            chart?.Refresh();
        }
    }
}